package top.zhimingw.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import top.zhimingw.pojo.Tag;
import top.zhimingw.redis.TagKey;
import top.zhimingw.service.BaseService;
import top.zhimingw.service.TagService;

import java.util.List;

/**
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/5/31 15:24
 */
@Service("tagService")
public class TagServiceImpl extends BaseService implements TagService {
    /**
    * @Description: 添加Tag
    * @Param: [tag]
    * @return: boolean
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public boolean insert(Tag tag) {
        if(tagMapper.insert(tag)==1){
            return true;
        }
        return false;
    }

    /**
    * @Description: 删除Tag
    * @Param: [id]
    * @return: boolean
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public boolean deleteById(Integer id) {
        if(tagMapper.deleteById(id)==1){
            return true;
        }
        return false;
    }

    /**
    * @Description: 查询单条数据
    * @Param: [id]
    * @return: top.zhimingw.pojo.Tag
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public Tag queryById(Integer id) {
        Tag tag = null;
        if(redisService.exists(TagKey.getById,String.valueOf(id))){
            tag = redisService.get(TagKey.getById,String.valueOf(id),Tag.class);
        }else{
            tag = tagMapper.queryById(id);
            if(!ObjectUtils.isEmpty(tag)){
                redisService.set(TagKey.getById,String.valueOf(id),tag,60*60*24);
            }
        }
        return tag;
    }

    /**
    * @Description:
    * @Param: []
    * @return: java.util.List<top.zhimingw.pojo.Tag>
    * @Author: zhimingw
    * @Date: 2021/5/31
    */
    @Override
    public List<Tag> queryAll() {
        List<Tag> tags = null;
        //获取所有标签
        if(redisService.exists(TagKey.getIndex,"")){
            tags = redisService.getList(TagKey.getIndex,"",Tag.class);
        }else{
            tags = tagMapper.queryAll();
            tags.forEach(tag->{
                tag.setArticleCount(articleTagMapper.getArticleCount(tag.getId()));
            });
            //放入缓存中
            if(!ObjectUtils.isEmpty(tags)){
                redisService.setList(TagKey.getIndex,"",tags);
            }
        }
        return tags;
    }

    /**
    * @Description: 实体作为筛选条件查询数据
    * @Param: [tag]
    * @return: java.util.List<top.zhimingw.pojo.Tag>
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public List<Tag> queryAll(Tag tag) {
        return tagMapper.queryAll(tag);
    }

    /**
    * @Description: 修改数据，哪个属性不为空就修改哪个属性
    * @Param: [tag]
    * @return: boolean
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public boolean update(Tag tag) {
        if(tagMapper.update(tag)==1){
            return true;
        }
        return false;
    }

    /**
    * @Description: 根据文章ID查询所有标签
    * @Param: [id]
    * @return: java.util.List<top.zhimingw.pojo.Tag>
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public List<Tag> queryByArticleId(Integer id) {
        return articleTagMapper.queryByArticleId(id);
    }

    /**
    * @Description: 根据标签ID获取关联文章的数量
    * @Param: [id]
    * @return: java.lang.Integer
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public Integer getArticleCount(Integer id) {
        return articleTagMapper.getArticleCount(id);
    }

    /**
    * @Description: 获取评论的数量
    * @Param: []
    * @return: java.lang.Integer
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public Integer getCount() {
        return tagMapper.getCount();
    }

}
